﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharpTeam.Crm.IsvCustomization;
using Microsoft.Crm.Sdk;
using Microsoft.Crm.Sdk.Query;
using Microsoft.Crm.SdkTypeProxy;
using Microsoft.Crm.SdkTypeProxy.Metadata;
using System.Xml;
using System.Net;

namespace SharpTeam.Crm.SkypeDialer.Configuration
{
    /// <summary>
    /// Класс для создания кнопок дозвона
    /// </summary>
    public class DialButtonsCreator
    {
        IsvConfigCustomizer customizer;

        public DialButtonsCreator(string orgName, ICredentials credentials, string serverUrl)
        {
            customizer = new IsvConfigCustomizer(orgName, serverUrl, credentials);
        }

        /// <summary>
        /// Создать кнопку дозвона на форме объекта
        /// </summary>
        /// <param name="entityName">Имя объекта, для которого создается кнопка</param>
        public void CreateDialButton(string entityName)
        {
            
            ToolBarButton button = new ToolBarButton() {
                AccessHotKey = 'D',
                AvailableOffline = ConfigurationBoolean.Undefined,
                Icon = "/ISV/sharp-team.com/_imgs/skype-icon.gif",
                PassParameters = ConfigurationBoolean.True,
                SupportedClients = ClientTypes.All,
                Url = "/ISV/sharp-team.com/Dialer.aspx",
                ValidForCreate = ConfigurationBoolean.False,
                ValidForUpdate = ConfigurationBoolean.True,
                WindowLaunchMode = WindowMode.ModalDialog,
                WindowParameters = ""
            };
            
            button.Title = "Skype";
            button.ToolTip = "Call or chat using Skype";
            // TODO Localized resources for button title and tooltip

            customizer.AddEntityToolBarButton(entityName, button);

        }

        /// <summary>
        /// Подтвердить изменения
        /// </summary>
        public void Commit()
        {
            customizer.SaveIsvConfig();
        }

        public void CreateStandardButtons()
        {
            CreateDialButton(EntityName.account.ToString());

            CreateDialButton(EntityName.contact.ToString());
            CreateDialButton(EntityName.lead.ToString());
            CreateDialButton(EntityName.phonecall.ToString());
        }
    }
}
